import {
    queryHistoryImage,
    spaceResourcesQuery,
    queryMap,
    mapSpecialQuery
  } from 'api/space';

  export default {
      namespace: 'space',
      state: {
        SpaceResource: [],
        SpaceResourceMap: [],
        SpaceResourceHistory: [],
        SpaceResourceUrl: []
      },

      effects: {
        *queryHistoryImage({ payload }, { call, put }) {
          const data = yield call(queryHistoryImage, payload)
          if (data && data.success) {
            yield put({
              type: 'setState',
              payload: {
                SpaceResourceHistory: data.data,
              },
            })
            return data;
          } else {
            throw data
            return data;
          }
        },
        *spaceResourcesQuery({ payload }, { call, put }) {
          const data = yield call(spaceResourcesQuery, payload)
          if (data && data.success) {
            yield put({
              type: 'setState',
              payload: {
                SpaceResource: data.data,
              },
            })
            return data;
          } else {
            throw data;
            return data;
          }
        },
        *arcgisFeatureServerQuery({ payload }, { call, put }) {
          const data = yield call(queryMap, payload)
          if (data && data.features) {
            yield put({
              type: 'setState',
              payload: {
                SpaceResourceMap: data.features,
              },
            })
            return data;
          } else {
            // throw data
            return data;
          }
        },
        *mapSpecialQuery({ payload }, { call, put }) {
          const data = yield call(mapSpecialQuery, payload)
          if (data && data.success) {
            yield put({
              type: 'setState',
              payload: {
                SpaceResourceUrl: data.data,
              },
            })
            return data;
          } else {
            // throw data
            return data;
          }
        },
      },

      reducers: {
        setState(state, action) {
          return {
              ...state,
              ...action.payload
          };
        }
      },
    }
